package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.BaseContext;
import com.itheima.dao.*;
import com.itheima.pojo.*;
import com.itheima.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrdersDao, Orders> implements OrderService {
    @Autowired
    private ShoppingCartDao shoppingCartDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private AddressBookDao addressBookDao;
    @Autowired
    private OrdersDao ordersDao;
    @Autowired
    private OrdersDetailDao ordersDetailDao;

    @Override
    public void submit(Orders orders) {
        //完成订单数据填充，然后清空购物车
        Long userid = BaseContext.getCurrentId();
        //查询用户购物车数据
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userid);
        List<ShoppingCart> shoppingCarts = shoppingCartDao.selectList(lqw);
        //查询用户数据
        User user = userDao.selectById(userid);
        //根据地址id查询地址数据
        AddressBook addressBook = addressBookDao.selectById(orders.getAddressBookId());

        long orderId = IdWorker.getId();//订单号
        BigDecimal amount = new BigDecimal(0);//总价

        //组装订单明细信息
        for (ShoppingCart shoppingCart : shoppingCarts) {
            OrderDetail orderDetail = new OrderDetail();
            BeanUtils.copyProperties(shoppingCart,orderDetail);//拷贝除orderId和amount的其他属性
            //设置orderId
            orderDetail.setOrderId(orderId);
            //设置总金额amount
            BigDecimal price = shoppingCart.getAmount();//单价
            BigDecimal number = new BigDecimal(shoppingCart.getNumber());//数量
            amount = price.multiply(number);//相乘得总价
            orderDetail.setAmount(amount);
            //向订单明细表插入数据，多条数据
            ordersDetailDao.insert(orderDetail);
        }

        //组装订单数据
        Orders newOrder = new Orders();
        BeanUtils.copyProperties(orders,newOrder);//拷贝备注，支付方式，地址id
        newOrder.setId(orderId);
        newOrder.setNumber(String.valueOf(orderId));
        newOrder.setStatus(1);
        newOrder.setUserId(userid);
        newOrder.setUserName(user.getName());
        newOrder.setOrderTime(LocalDateTime.now());
        newOrder.setCheckoutTime(LocalDateTime.now());
        newOrder.setAmount(amount);
        newOrder.setPhone(user.getPhone());
        newOrder.setAddress(addressBook.getDetail());
        newOrder.setConsignee(addressBook.getConsignee());
        orders.setAddress(
                (addressBook.getProvinceName() == null ? "" : addressBook.getProvinceName())
                        + (addressBook.getCityName() == null ? "" : addressBook.getCityName())
                        + (addressBook.getDistrictName() == null ? "" : addressBook.getDistrictName())
                        + (addressBook.getDetail() == null ? "" : addressBook.getDetail()));//省+市+区域+详细地址
        //向订单表插入数据，一条数据
        ordersDao.insert(newOrder);

        //清空购物车数据
        shoppingCartDao.delete(lqw);
    }
}